package com.franklin.plugins.common.utils;



import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 正则表达式工具类
 *
 * @author Franklin
 * @since 2021/8/3 10:35
 */
public class PatternUtils {

    public final static String IMPLEMENT_PATTERN
            = "^\\s*new\\s+(?<implementType>.*)\\s*\\([\\s\\S]*\\)\\s*$";

    //Integer
    public final static String INT_PATTERN = "^\\d*$";
    //Long
    public final static String LONG_PATTERN = "^\\d*[Ll]$";
    //Double
    public final static String DOUBLE_PATTERN = "^(-?\\d+)(\\.\\d+)?[Dd]$";
    //Float
    public final static String FLOAT_PATTERN = "^(-?\\d+)(\\.\\d+)?[Ff]$";
    //String
    public final static String STRING_PATTERN = "^\"[\\s\\S]*\"$";

    public final static String EL_PARAM_PATTERN
            = "[\\s\\S]*(?<elExpression>(#|$)\\s*\\{\\s*%s\\s*(\\,\\s*jdbc(t|T)ype\\s*=\\s*\\w+)?\\})[\\s\\S]*$";


    public static String findElExpression(String target,String elParam){
        String regex = String.format(EL_PARAM_PATTERN, elParam);
        Map<String, String> elExpression = find(regex, target, "elExpression");
        return elExpression.get("elExpression");
    }

    public static String findImplementType(String declaration){
        Map<String, String> implementTypeResult = PatternUtils.find(IMPLEMENT_PATTERN, declaration, "implementType");
        return implementTypeResult.getOrDefault("implementType", "");
    }

    /**
     * 根据变量名寻找变量值<br>
     *
     * @param regex:
     * @param input:
     * @param names:
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author Franklin
     * @since 2021/8/3 10:50
     */
    public static Map<String, String> find(String regex, String input, Iterable<String> names) {
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()) {
                Map<String, String> result = new HashMap<>();
                names.forEach(name -> {
                    String value = matcher.group(name);
                    if (DataCheckUtils.isNotEmpty(value)) {
                        result.put(name, value);
                    }
                });
                return result;
            }
        } catch (Exception e) {
            System.err.println("pattern match error : ");
            System.err.println("regex = " + regex);
            System.err.println("input = " + input);
            System.err.println();
        }
        return Collections.emptyMap();
    }

    public static Map<String, String> find(String regex, String input, String... names) {
        return find(regex, input, Arrays.asList(names));
    }

    public static boolean isStringValue(String value) {
        return match(STRING_PATTERN, value);
    }

    public static boolean isIntValue(String value) {
        return match(INT_PATTERN, value);
    }

    public static boolean isLongValue(String value) {
        return match(LONG_PATTERN, value);
    }

    public static boolean isFloatValue(String value) {
        return match(FLOAT_PATTERN, value);
    }

    public static boolean isDoubleValue(String value) {
        return match(DOUBLE_PATTERN, value);
    }

    public static boolean isBooleanValue(String value) {
        if (DataCheckUtils.isNotEmpty(value)) {
            return value.equals("true") || value.equals("false");
        }
        return false;
    }

    public static boolean match(String regex, String input) {
        if (DataCheckUtils.isNotEmpty(input)) {
            return Pattern.matches(regex, input);
        }
        return false;
    }

}
